package journal

import "time"

type fieldType uint8

const (
	FieldTypeUnknown fieldType = iota
	FieldTypeInt8
	FieldTypeInt16
	FieldTypeBoolean
	FieldTypeInt32
	FieldTypeInt64
	FieldTypeInt
	FieldTypeFloat32
	FieldTypeFloat64
	FieldTypeString
	FieldTypeTime
	FieldTypeDuration
	FieldTypeAny
	FieldTypeError
)

type Field struct {
	Key   string
	Value any
	Type  fieldType
}

func String(key string, value string) Field {
	return Field{
		Key:   key,
		Value: value,
		Type:  FieldTypeString,
	}
}

func Int(key string, value int) Field {
	return Field{
		Key:   key,
		Value: value,
		Type:  FieldTypeInt,
	}
}

func Int8(key string, value int8) Field {
	return Field{
		Key:   key,
		Value: value,
		Type:  FieldTypeInt8,
	}
}

func Int16(key string, value int16) Field {
	return Field{
		Key:   key,
		Value: value,
		Type:  FieldTypeInt16,
	}
}

func Int32(key string, value int32) Field {
	return Field{
		Key:   key,
		Value: value,
		Type:  FieldTypeInt32,
	}
}

func Int64(key string, value int64) Field {
	return Field{
		Key:   key,
		Value: value,
		Type:  FieldTypeInt64,
	}
}

func Float32(key string, value float32) Field {
	return Field{
		Key:   key,
		Value: value,
		Type:  FieldTypeFloat32,
	}
}

func Float64(key string, value float64) Field {
	return Field{
		Key:   key,
		Value: value,
		Type:  FieldTypeFloat64,
	}
}

func Boolean(key string, value bool) Field {
	return Field{
		Key:   key,
		Value: value,
		Type:  FieldTypeBoolean,
	}
}

func Time(key string, value time.Time) Field {
	return Field{
		Key:   key,
		Value: value,
		Type:  FieldTypeTime,
	}
}

func Duration(key string, value time.Duration) Field {
	return Field{
		Key:   key,
		Value: value,
		Type:  FieldTypeDuration,
	}
}

func Any(key string, value any) Field {
	return Field{
		Key:   key,
		Value: value,
		Type:  FieldTypeAny,
	}
}

func Error(err error) Field {
	if err == nil {
		return Field{
			Type: FieldTypeUnknown,
		}
	}
	return Field{
		Key:   "error",
		Value: err,
		Type:  FieldTypeError,
	}
}
